//
//  NumberViewController.swift
//  DemoRecord
//
//  Created by 郝佳佳 on 16/9/6.
//  Copyright © 2016年 郝佳佳. All rights reserved.
//

import UIKit

class NumberViewController: UIViewController {

    var number: NSNumber?
    
    override func viewDidLoad() {
        super.viewDidLoad()
                
    }

  

}


extension NumberViewController{
    
    func testNumber(){
        
        roundf(11.123)
        roundf(11.123 / 100 * 100)
        
        serializationJsonNumber("{\"number\":8.3}")
        serializationJsonNumber("{\"number\":8.03}")
        serializationJsonNumber("{\"number\":8.033}")
        serializationJsonNumber("{\"number\":8.322}")
        serializationJsonNumber("{\"number\":66.1234}")
        serializationJsonNumber("{\"number\":66.1234567}")
        serializationJsonNumber("{\"number\":66.012}")
        serializationJsonNumber("{\"number\":1.012}")
        serializationJsonNumber("{\"number\":6.012}")
        serializationJsonNumber("{\"number\":6.123}")
        serializationJsonNumber("{\"number\":11.012}")
        serializationJsonNumber("{\"number\":1.1234567}")
        serializationJsonNumber("{\"number\":1.0123456}")
        serializationJsonNumber("{\"number\":1.12345678}")
        serializationJsonNumber("{\"number\":1.01234567}")

    }
    
    /*      JavaScript中所有的数字，无论是整数还是小数，其类型均为Number。在程序内部，Number类型的实质是一个64位的浮点数，这与Java中double类型的浮点数是一致的；因此，JavaScript中所有的数都是浮点数。遵循IEEE 754标准（浮点数算术标准），JavaScript所能表示的数值范围为正负1.7976931348623157乘以10的308次方，其最小所能表示的小数为正负5乘以10的负324次方，这两个边界值可以分别通过访问Number对象的MAX_VALUE属性和MIN_VALUE属性来获取。
     double转化为NSNumber的时候就会出现问题。double表示的字面值和其存储的值一般是不一样的*/

    func serializationJsonNumber(_ json: String){
//        let model = Model.mj_object(withKeyValues: json)
//        print(model?.number?.stringValue)
//      //        let json = "{\"number\":66.1234}"
//        let data = json.data(using: String.Encoding.utf8)
////         let data =  try? NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)
//        do{
//            if let dict = try! JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as? [String: AnyObject]{
//                let dictValue = dict["number"]
//                print("\(dict) => \(dictValue?.typeClass) \(dictValue?.isKVCDisabled) \(dictValue?.isNumberType)")
//                setValuesForKeys(dict)
//                let numStr = NSDecimalNumber(value: number!.doubleValue as Double).stringValue
//                print("numStr => \(numStr)")
//            }
//        }
//        print("\n")

    }
    
    func demo(){
/*NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
 
 [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
 [formatter setMaximumFractionDigits:2];
 [formatter setRoundingMode: NSNumberFormatterRoundUp];
 
 NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:22.368511]];
 
 NSLog(@"Result...%@",numberString); */
        let number1 = NSNumber(value: 1113332.2 as Double)
        let number2 = NSNumber(value: 222442.2345 as Double)
        let number3 = NSNumber(value: 222442.2375 as Double)


        let formatter = NumberFormatter()
//        formatter.positiveFormat = "###0.##"
        formatter.numberStyle = .decimal
        formatter.minimumFractionDigits = 2
        formatter.maximumFractionDigits = 2
        
//        formatter.setPositiveFormat = "###0.##"//"0.##"
//        [fmt setPositiveFormat:@"0.##"];
        

//        formatter.setNumberStyle = NSNumberFormatterDecimalStyle
//        formatter.setMaximumFractionDigits = 2
        print(formatter.string(from: number1))
        print(formatter.string(from: number2))
        print(formatter.string(from: number3))

 
    }
    
    func convertStringAndNSNumber(){
        
//        let str = "111.012345"
//        print("Double====\(Double(str)!)")
//        print("float====\(Float(str)!)")
//        print("NSNumber_D====\(NSNumber.init(double: Double(str)!))")
//        print("NSNumber_F====\(NSNumber.init(float: Float(str)!))")
        
        let num: NSNumber = 222.123456
        let str = "\(num.doubleValue)"
        let numFromString = NSNumber.init(value: Double(str)! as Double)
        print("NSNumber===\(num)")
        print("String===\(str)  \(num.stringValue)")
        print("NSNumber_ByChange===\(numFromString)")
        print(num.stringValue)
        
        let float: Float = 333.123456
        let numFromFloat = NSNumber.init(value: float as Float)
        print("Float===\(numFromFloat)")
        
        let double: Double = 8.123
        let numFromDouble = NSNumber.init(value: double as Double)
        let stringFromNumber = numFromDouble.stringValue
        let stringFromDouble = NSString.init(format: "%lf", double)
        let decimalNumber = NSDecimalNumber.init(string: stringFromDouble as String)
        print("double===\(double)")
        print("stringFromNumber===\(stringFromNumber)")
        print("decimalNumber===\(decimalNumber)")
        /*double d2             = 8.3;
         NSString *d2Str       = [NSString stringWithFormat:@"%lf", d2];
         NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:d2Str];
         NSString *strD2       = [num1 stringValue];*/
    }
}
